package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.NameDouble;
import com.kaizzi.data.common.been.SeriesData;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Component
@Slf4j
public class Template32 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if (textParagraphs == null || textParagraphs.size() == 0) {
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if (textParagraph == null) {
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if (textRuns == null) {
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject() == null) {
                    return;
                }
                try {
                    if (textRun.getRawText().equals("24KEY-1")) {
                        String title = data.get("title").asText();
                        templateUtil.text(textParagraph, title, false);
                        break;
                    }
                    if (textRun.getRawText().equals("24KEY-3")) {
                        String dataPeriod = data.get("dataPeriod").asText();
                        textRun.setText(dataPeriod);
                        break;
                    }
                } catch (Exception e) {
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text Exception:{}", 32, e);
                }
            }
        }
    }

    public void barChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        String jsonKey = "areas";
        if (plot.getBarChartList().get(0).getSerList().get(0).getVal().getNumRef().getNumCache().getPtArray(0).getV().equals("261")){
            jsonKey = "ages";
        }
        List<SeriesData> barData = getData(data, jsonKey);
        templateUtil.barChart(plot, barData, workbook, sheet, chart);
    }

    private static List<SeriesData> getData(JsonNode jsonNode, String jsonKey) {
        JsonNode data = jsonNode.get(jsonKey);
        List<SeriesData> seriesDatas = new ArrayList<>();
        List<NameDouble> nameDoubles = new ArrayList<>();
        for (JsonNode dateJson : data) {
            NameDouble playerDouble = new NameDouble(dateJson.get("tempName").asText(), dateJson.get("tempCount").asDouble());
            nameDoubles.add(playerDouble);
        }
        SeriesData playerSeriesData = new SeriesData("name", nameDoubles, "");
        seriesDatas.add(playerSeriesData);
        return seriesDatas;
    }

    public void pieChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        String jsonKey = "areas";
        if (plot.getPieChartList().get(0).getSerList().get(0).getVal().getNumRef().getNumCache().getPtArray(0).getV().equals("10")) {
            jsonKey = "genders";
        }
        List<SeriesData> pieData = getData(data, jsonKey);
        templateUtil.pieChart(plot, pieData, workbook, sheet, chart);
    }
}
